import { http as axios } from './request'
import { appId } from '../utils/config'

/**
 * 换取手机号
 * @param code 
 * @param encryptedData 
 * @param iv 
 */
export const getUserPhone = (encryptedData: string, iv: string, sessionKey: string) => {
  return axios({
    method: 'GET',
    url: `/api/user/wxma/${appId}/phoneInfo`,
    params: {
      encryptedData, iv, sessionKey
    }
  })
}
export const getWXUserInfo = (encryptedData: string, iv: string, sessionKey: string) => {
  return axios({
    method: 'GET',
    url: `/api/user/wxma/${appId}/userInfo`,
    params: {
      encryptedData, iv, sessionKey
    }
  })
}
//使用 code 换取 openid、unionid、session_key 等信息
export const wxLogin = (code: string) => {
  return axios({
    method: 'GET',
    url: `/api/user/wxma/${appId}/sessionInfo`,
    params: { code }
  })
}

/**
 * 获取用户信息
 */
export const getUserInfo = () => {
  return axios({
    method: 'GET',
    url: `/api/user/portalUser/info`
  })
}

/**
 * 更新用户信息
 * @param avatar 
 * @param nickname 
 * @param openId 
 */
export const updateUserInfo = (avatar: string, nickname: string, openId?: string) => {
  return axios({
    method: 'POST',
    url: '/api/user/portalUser/update/info',
    data: {
      avatar,
      nickname,
      openId,
    }
  })
}
